const express = require("express");
// const moment = require('moment')
const router = express.Router();
const db = require("../core/mysql");
router.get("/getlist", async (req, res, next) => {
  let sqlStr = `SELECT * FROM textlist`;
  db.query(sqlStr, (err, result) => {
    console.log("---------err", err);
    console.log("---------result", result);
    if (err) {
      res.json({
        msg: "服务器错误",
        code: 500,
      });
      return;
    }
    if (result.length >= 1) {

      res.json(result);
    } else {
      1
      res.json({
        msg: "参数错误",
        code: 201,
      });
    }
  });
});
router.post("/addlist", async (req, res, next) => {
  let { textBody, textAuthor, textCreatedate, textTheme } = req.query;
  if (!textBody || !textAuthor || !textCreatedate || !textTheme) {
    return res.json({
      msg: "请核对参数",
      code: 500,
    });
  }
  const sqlStr = `INSERT INTO textlist (text_body,text_author,text_createdate,text_theme) VALUES('${textBody}','${textAuthor}','${textCreatedate}','${textTheme}');`;
  db.query(sqlStr, (err, result) => {
    console.log("---------err", err);
    console.log("---------result", result);
    if (err) {
      res.json({
        msg: "服务器错误",
        code: 500,
      });
      return;
    }
    if (result.length >= 1) {
      res.json({
        msg: "查询成功",
        code: 200,
        data: result,
      });
    } else {
      res.json({
        msg: "参数错误",
        code: 201,
      });
    }
  });
});
module.exports = router;
